#include "gameboard.h"
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSimpleTextItem>
#include <QLinearGradient>

GameBoard::GameBoard(QWidget *parent)
    : QGraphicsView(parent),
    m_scene(new QGraphicsScene(this))
{
    setScene(m_scene);
    setRenderHint(QPainter::Antialiasing);
    setMinimumSize(600, 600);
    setStyleSheet("border: none; background: transparent;");

    initializeSpecialGrids();
    createGrid();

    // 初始化玩家棋子
    const QColor colors[] = {Qt::red, Qt::blue, Qt::green};
    for(int i = 0; i < 3; ++i) {
        m_players[i] = new QGraphicsEllipseItem(0, 0, 40, 40);
        m_players[i]->setBrush(colors[i]);
        m_players[i]->setPen(QPen(Qt::black, 1));
        m_players[i]->setZValue(1);
        m_scene->addItem(m_players[i]);
    }
    reset();
}

void GameBoard::initializeSpecialGrids() {
    m_specialGrids = {
        {45, BackToStart}, {63, BackToStart},
        {7, ExtraTurn},    {18, ExtraTurn}, {29, ExtraTurn}, {52, ExtraTurn}, {79, ExtraTurn},
        {5, Forward3},     {11, Forward3},  {21, Forward3},  {35, Forward3},  {68, Forward3},
        {6, Backward5},    {22, Backward5}, {44, Backward5}, {58, Backward5}, {74, Backward5},
        {66, TeleportEnd}
    };
}

void GameBoard::createGrid() {
    QLinearGradient bgGradient(0, 0, width(), height());
    bgGradient.setColorAt(0, QColor(240, 240, 240));
    bgGradient.setColorAt(1, QColor(200, 200, 200));
    m_scene->setBackgroundBrush(bgGradient);

    // 创建棋盘格子
    for(int row = 0; row < BOARD_SIZE; ++row) {
        for(int col = 0; col < BOARD_SIZE; ++col) {
            int pos = row * BOARD_SIZE + ((row % 2 == 0) ? col : (BOARD_SIZE - 1 - col));

            QGraphicsRectItem* cell = new QGraphicsRectItem(
                col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE);
            cell->setPen(QPen(Qt::gray, 1));
            cell->setBrush(QColor(255, 255, 255, 150));
            m_scene->addItem(cell);

            QGraphicsSimpleTextItem* numText = new QGraphicsSimpleTextItem(QString::number(pos));
            numText->setPos(col * GRID_SIZE + 5, row * GRID_SIZE + 5);
            numText->setBrush(Qt::darkGray);
            m_scene->addItem(numText);
        }
    }
}

QPointF GameBoard::calculatePosition(int pos) const {
    int row = pos / BOARD_SIZE;
    bool isReverseRow = row % 2 != 0;
    int col = isReverseRow ? (BOARD_SIZE - 1 - pos % BOARD_SIZE) : pos % BOARD_SIZE;

    qreal baseX = col * GRID_SIZE + (GRID_SIZE - 40) / 2.0;
    qreal baseY = row * GRID_SIZE + (GRID_SIZE - 40) / 2.0;
    return QPointF(baseX, baseY);
}

void GameBoard::movePlayer(int playerId, int position) {
    if(playerId < 0 || playerId > 2) return;
    position = qBound(0, position, 80);
    m_players[playerId]->setPos(calculatePosition(position));
}

void GameBoard::setPlayerVisible(int playerId, bool visible) {
    if(playerId >=0 && playerId <3)
        m_players[playerId]->setVisible(visible);
}

void GameBoard::reset() {
    for(int i = 0; i < 3; ++i) {
        movePlayer(i, 0);
        setPlayerVisible(i, i < 2);
    }
}

GridType GameBoard::getGridType(int position) const {
    return m_specialGrids.value(position, Normal);
}
